import Mock from 'mockjs'
import * as PublicConfiguration from '@utils/PublicConfiguration'; // 使用 * as 导入整个模块
import Components from './Components'

/**
 * @date: 2024-09-28 01:44:09
 * @description: 用于存储用户表格数据
 * @modifyContent:
 * @author: Forever丿顾北
**/
let UserTableList = []

/**
 * @date: 2024-09-28 01:37:59
 * @description: 获取User表格数据
 * @modifyContent:
 * @author: Forever丿顾北
**/
const getUserTableData = (config) => {
  if (UserTableList.length === 0) {
    for(let i = 0; i < Components.count; i++) {
      UserTableList.push(
        Mock.mock({
          id: Mock.Random.guid(),
          name: Mock.Random.cname(),
          'age|18-50': 1,
          gender: Mock.Random.integer(0, 1),
          birthDate: Mock.Random.date('yyyy-MM-dd'),
          address: Mock.mock('@county(true)')
        })
      )
    }
  }
  const { name, page = 1, limit = PublicConfiguration.limit } = Components.Param2Obj(config.url)
  const MockList = UserTableList.filter(item => {
    if(name && item.name.indexOf(name) === -1 && item.address.indexOf(name) === -1) return false
    return true
  })
  
  const PageMockList = MockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

  return {
    code: 200,
    msg: '获取成功',
    data: {
      tableData: PageMockList,
      total: MockList.length
    }
  }
}


/**
 * @date: 2024-09-28 02:26:09
 * @description:新增用户数据
 * @author: Forever丿顾北
 * @param { String | name } 姓名
 * @param { Number | age} 年龄
 * @param { String | gender} 性别
 * @param { Date | birthDate(YYYY-MM-DD) } 出生年月
 * @param { String | address} 住址
**/
const AddUserTableData = (config) => {
  const { name, age, gender, birthDate, address } = JSON.parse(config.body)
  UserTableList.unshift({
    id: Mock.Random.guid(),
    name,
    age,
    gender,
    birthDate,
    address
  })
  return {
    code: 200,
    msg: '新增成功',
    data: {}
  }
}

/**
 * @date: 2024-09-28 02:32:26
 * @description:编辑用户数据
 * @author: Forever丿顾北
 * @param { String | id } id
**/
const EditUserTableData = (config) => {
  const { id, name, age, gender, birthDate, address } = JSON.parse(config.body)
  console.log('1qw', id, name, age, gender, birthDate, address)
  const sex_num = parseInt(gender)
  UserTableList.some(item => {
    if(item.id === id){
      item.name = name
      item.age = age
      item.gender= sex_num
      item.birthDate = birthDate
      item.address = address
      return true
    }
    return true
  })
  return {
    code: 200,
    msg: '编辑成功',
    data: {}
  }
}
/**
 * @date: 2024-09-28 01:38:23
 * @description: 删除User表格数据
 * @author: Forever丿顾北
 * @param { String | id } id
**/
const deleteUserTableData = (config) => {
  const { id } = JSON.parse(config.body)
  if(!id) {
    return {
      code: -999,
      msg: '参数不正确',
      data: {}
    }
  } else {
    UserTableList = UserTableList.filter(item => item.id !== id)
    return {
      code: 200,
      msg: '删除成功',
      data: {}
    }
  }
}

 const User = {
  getUserTableData, // 获取User表格数据
  AddUserTableData, // 新增用户数据
  EditUserTableData, // 编辑用户数据
  deleteUserTableData // 删除User表格数据
};

export default User;